package com.qyt.hotel.controller;

import com.qyt.hotel.entity.Result;
import com.qyt.hotel.entity.Role;
import com.qyt.hotel.entity.StatusCode;
import com.qyt.hotel.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @GetMapping("/getOne")
    public Role getOne(Integer roleId) {
        return roleService.getOne(roleId);
    }

    @GetMapping("/list")
    public Result getRoleList() {
        List<Role> roleList = roleService.getRoleList();

        if (null != roleList) {
            return new Result("查询成功", true, StatusCode.OK, roleList);
        } else {
            return new Result<>("查询失败", false, StatusCode.ERROR, null);
        }
    }

    @PostMapping("/add")
    public Result addRole(Role role) {
        Integer result = roleService.addRole(role);
        if (null != result) {
            return new Result("添加成功", true, StatusCode.OK, null);
        } else {
            return new Result<>("添加失败", false, StatusCode.ERROR, null);
        }
    }

    @PostMapping("/delete")
    public Result deleteRole(Role role) {
        Integer result = roleService.deleteRole(role);
        if (null != result) {
            return new Result("删除成功", true, StatusCode.OK, null);
        } else {
            return new Result<>("删除失败", false, StatusCode.ERROR, null);
        }

    }

    @PostMapping("/update")
    public Result updateRole(Role role) {
        Integer result = roleService.updateRole(role);
        if (null != result) {
            return new Result("修改成功", true, StatusCode.OK, null);
        } else {
            return new Result<>("修改失败", false, StatusCode.ERROR, null);
        }
    }
}
